package cn.edu.sgu.www.mhxysy.controller.waiguan;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.waiguan.ShizhuangCategoryDTO;
import cn.edu.sgu.www.mhxysy.dto.waiguan.ShizhuangCategoryInsertBatchDTO;
import cn.edu.sgu.www.mhxysy.entity.waiguan.ShizhuangCategory;
import cn.edu.sgu.www.mhxysy.pager.waiguan.ShizhuangCategoryPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.waiguan.ShizhuangCategoryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "时装类型管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/shizhuang_category", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class ShizhuangCategoryController {

    private final ShizhuangCategoryService shizhuangCategoryService;

    @Autowired
    public ShizhuangCategoryController(ShizhuangCategoryService shizhuangCategoryService) {
        this.shizhuangCategoryService = shizhuangCategoryService;
    }

    @ApiOperation("上传图片")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public JsonResult<String> upload(MultipartFile file) throws IOException {
        String url = shizhuangCategoryService.upload(file);

        return JsonResult.success(CustomMessages.UPLOAD_SUCCESS, url);
    }

    @ApiOperation("添加时装类型")
    @ApiOperationSupport(ignoreParameters = "id")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated ShizhuangCategoryDTO shizhuangCategoryDTO) {
        shizhuangCategoryService.insert(shizhuangCategoryDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("批量添加时装类型")
    @RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
    public JsonResult<Void> insertBatch(@Validated ShizhuangCategoryInsertBatchDTO insertBatchDTO) {
        shizhuangCategoryService.insertBatch(insertBatchDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID删除时装类型")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public JsonResult<Void> deleteById(
            @ApiParam(name = "id", value = "时装类型ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        shizhuangCategoryService.deleteById(id);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改时装类型信息")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated ShizhuangCategoryDTO shizhuangCategoryDTO) {
        shizhuangCategoryService.updateById(shizhuangCategoryDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过ID修改时装图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "装备类型ID", required = true),
            @ApiImplicitParam(name = "image", value = "图片地址", required = true)
    })
    @RequestMapping(value = "/updateImageById", method = RequestMethod.POST)
    public JsonResult<Void> updateImageById(
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id,
            @NotBlank(message = CustomMessages.IMAGE_NOT_NULL) String image) {
        shizhuangCategoryService.updateImageById(id, image);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("查询全部时装类型")
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public JsonResult<List<ShizhuangCategory>> selectAll() {
        List<ShizhuangCategory> list = shizhuangCategoryService.selectAll();

        return JsonResult.success(null, list);
    }

    @ApiOperation("查询父级时装类型")
    @RequestMapping(value = "/selectParent", method = RequestMethod.GET)
    public JsonResult<List<ShizhuangCategory>> selectParent() {
        List<ShizhuangCategory> list = shizhuangCategoryService.selectParent();

        return JsonResult.success(null, list);
    }

    @ApiOperation("查询时装类型列表")
    @RequestMapping(value = "/selectList", method = RequestMethod.GET)
    public JsonResult<List<ShizhuangCategory>> selectList(
            @ApiParam(name = "gender", value = "适用性别", required = false)
            @RequestParam(required = false) Integer gender) {
        List<ShizhuangCategory> list = shizhuangCategoryService.selectList(gender);

        return JsonResult.success(null, list);
    }

    @ApiOperation("通过ID查询时装类型信息")
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public JsonResult<ShizhuangCategory> selectById(
            @ApiParam(name = "id", value = "时装类型ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        ShizhuangCategory shizhuangCategory = shizhuangCategoryService.selectById(id);

        return JsonResult.success(null, shizhuangCategory);
    }

    @ApiOperation("查询子类型列表")
    @RequestMapping(value = "/selectByParentId", method = RequestMethod.GET)
    public JsonResult<List<ShizhuangCategory>> selectByParentId(
            @ApiParam(name = "id", value = "父级类型ID", required = false)
            @RequestParam(required = false) Integer parentId) {
        List<ShizhuangCategory> list = shizhuangCategoryService.selectByParentId(parentId);

        return JsonResult.success(null, list);
    }

    @ApiOperation("分页查询时装类型列表")
    @RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
    public JsonResult<JsonPage<ShizhuangCategory>> selectByPage(ShizhuangCategoryPager pager) {
        Page<ShizhuangCategory> page = shizhuangCategoryService.selectByPage(pager);

        return JsonResult.restPage(page);
    }

}